﻿using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Sitecore.Configuration;
using Sitecore.Data;
using System;

using Sitecore.Data.Archiving;
using Sitecore.Data.Indexing;

namespace Cognifide.PowerShell.Shell.Commands
{
    [Cmdlet("Get", "Index", DefaultParameterSetName = "Name")]
    public class GetIndexCommand : DatabaseContextBaseCommand
    {
        protected override void ProcessRecord(IEnumerable<Database> databases)
        {
            if (String.IsNullOrEmpty(Name))
            {
                foreach (var database in databases)
                {
                    WriteObject(database.Indexes, true);
                }
            }
            else
            {

                foreach (var database in databases)
                {
                    List<Index> indices = new List<Index>(database.Indexes.Count);
                    for (int i = 0; i < database.Indexes.Count; i++)
                    {
                        indices.Add(database.Indexes[i]);
                    }
                    WildcardWrite(Name, indices, index => index.Name);
                }
            }

        }

    }
}